home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-02
/
hash.zip
/
LIST.7
< prev
Wrap
Text File
|
1993-01-04
|
4KB
|
101 lines
Program Search_With_Double_Hashing;
Const
max_TAB_entry = 60; {last TAB entry}
number_TAB_entries = 61; {number of entries in TAB}
empty = ' '; {what an empty entry looks like}
p_prime = 59; {first twin prime-used to calculate increment}
p = 61; {second twin prime-used to hash KEY}
Type
string4 = string[4];
Var
found: boolean; {set true by search if KEY is found}
index: integer; {pointer to the TAB entry being examined}
KEY: string4; {name to found or entered}
i: integer; {for FOR loop use}
n: integer; {number of entries currently in TAB}
TAB: array[ 0 .. max_TAB_entry ] of string4;
Procedure Search( KEY: string4 );
Function h( KEY: string4; modulus: integer ): integer;
Type
KEY_types = (char_KEY, integer_KEY);
KEY_overlay = record
case KEY_types of
char_KEY: ( KEY_in_characters: string4);
integer_KEY: ( dummy: byte; {takes up room for string size}
integer_KEY_1: integer; {first 2 bytes}
integer_KEY_2: integer; {last 2 bytes} );
end;
Var
KEY_record: KEY_overlay;
begin {h}
with KEY_record do
begin
KEY_in_characters := ' '; {in case KEY < 4 chars}
KEY_in_characters := KEY;
h := ( integer_KEY_1 xor integer_KEY_2 ) mod modulus;
end;
end; {h}
Procedure add_KEY_to_TAB;
begin {add_KEY_to_TAB}
n := n + 1; {one more entry in TAB}
if n > max_TAB_entry then {table is full}
begin
writeln(' ***Fatal Error***');
writeln('Table overflow in table TAB');
writeln(' program aborted');
halt; {stop with a fatal error}
end
else {there's still room, so add another entry}
TAB[ index ] := KEY;
end; {add_KEY_to_TAB}
Var
j: integer; {increment for current KEY}
begin {search}
found := false;
index := h( KEY, p ); {go hash KEY}
if TAB[ index ] = KEY then {found it}
found := true
else {we have to do some more looking}
begin
if TAB[ index ] = empty then {it's not there - enter it}
add_KEY_to_TAB
else
begin
j := h( KEY, p_prime ) + 1; {calculate the increment}
repeat
index := index + j; {step index to next entry}
if index > max_TAB_entry then {off the end of TAB}
index := index - number_TAB_entries; {make circular}
if TAB[ index ] = KEY then {we found it}
found := true; {so say so}
until ( TAB[ index ] = empty ) or found;
if not found then {we need to enter KEY}
add_KEY_to_TAB; {so do so}
end;
end;
end; {search}
Begin {Search_With_Double_Hashing}
n := 0; {no entries in TAB yet}
for i := 0 to max_TAB_entry do TAB[ i ] := empty; {all entries available}
{user code goes here}
End. {Search_With_Double_Hashing}